package com.java.test4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author luzhikun
 * @date 2020/12/20-21:20
 * <p>
 * try-catch-finally的使用
 * <p>
 * 1.finally是可选的
 * 2.finally中声明的是一定会被执行的代码。即使catch中出现异常了，try中由return语句，catch中由return语句，finally也一定会被执行
 * 3.像数据库，输入输出流、网络编程Socket等资源，JVM是不能自动回收的，我们需要手动的进行资源的释放，此时的资源释放，就需要声明在finally中。
 */
public class Finallytest {
    public static void main(String[] args) {
        Finallytest finallytest = new Finallytest();
        finallytest.test();
        try {
            int a = 10;
            int b = 0;
            System.out.println(a / b);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("我好帅啊");
        }
//        test();
    }

    public static void test() {
        FileInputStream fis = null;
        try {
            File file = new File("helowold");
            fis = new FileInputStream(file);
            int data = fis.read();
            while (data != -1) {
                System.out.println((char) data);
                data = fis.read();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("我是老二");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("我是老三");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("我是老四");
        } finally {
            try {
                if (fis != null)
                    fis.close();
                System.out.println("我是七妹");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("我是老大");
            } catch (Exception e) {
                System.out.println("我才是真正的错误");
            }


        }


    }
}
